module.exports = class extends think.Controller {
  async __before() {
    // 根据token值获取用户id
    this.ctx.state.token = this.ctx.header['access-token'] || '';
    const tokenSerivce = think.service('token', 'api');
    this.ctx.state.userId = await tokenSerivce.getUserId(this.ctx.state.token);

    // console.log(this.ctx.state.userId);

    // 只允许登录操作
    // if (this.ctx.controller !== 'auth') {
    //   if (this.ctx.state.userId <= 0) {
    //     return this.fail(401, '请先登录');
    //   }
    // }
  }

  getLoginUserId() {
    return this.ctx.state.userId;
  }
};
